package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.Api;
import com.example.demo.entity.ApiRequestResult;
import com.example.demo.entity.testData.AlarmData;
import com.example.demo.entity.testData.ConfigData;
import com.example.demo.entity.testData.DataTest;
import com.example.demo.entity.ResponseInfo;
import com.example.demo.entity.testData.FaultData;
import com.example.demo.service.ApiRequestResultService;
import com.example.demo.service.ApiService;
import com.example.demo.utils.PageInfoNew;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Validated
@RequestMapping("/api")
@io.swagger.annotations.Api(value = "/api", tags = "api接口")
public class ApiController {

    @Resource
    ApiService apiService;
    @Resource
    ApiRequestResultService apiRequestResultService;

    /**
     * 新增api接口
     *
     * @param api 传入api信息
     * @return 获取返回结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加api接口")
    public ResponseInfo<String> insertApi(@RequestBody Api api) {
        List<Api> apiList = apiService.findAllByName(api.getName());
        if (apiList.size() > 0) {
            return ResponseInfo.errorInfo("接口:" + api.getName() + "已存在");
        }
        apiService.insertApi(api);
        return ResponseInfo.successInfo("");
    }


    /**
     * api分页接口
     *
     * @param pageNum    页数
     * @param pageSize   每页长度
     * @param apiSuiteId api类别的id
     * @param projectId  项目id
     * @return 获取返回结果
     */
    @GetMapping("/page")
    @ApiOperation(value = "api分页接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度", required = true, dataType = "int"),
            @ApiImplicitParam(name = "apiSuiteId", value = "api类别的id", required = false, dataType = "int"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = false, dataType = "int")
    })
    public ResponseInfo<PageInfoNew<Api>> apiPage(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam(value = "apiSuiteId", required = false) Integer apiSuiteId,
                                                  @RequestParam(value = "apiName", required = false) String apiName,
                                                  @RequestParam(value = "projectId", required = false) Integer projectId) {
        return ResponseInfo.successInfo(apiService.findAllWithPage(pageNum, pageSize, apiSuiteId, apiName, projectId));
    }


    /**
     * api编写接口
     *
     * @param api 传入api信息
     * @return 获取返回结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "api更新接口")
    public ResponseInfo<String> updateApi(@RequestBody Api api) {
        try {
            apiService.updateApi(api);
            return ResponseInfo.successInfo("");
        } catch (Exception e) {
            return ResponseInfo.errorInfo(e.getMessage());
        }
    }

    /**
     * @param id 传入apiId
     * @return 获取返回结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除api接口")
    public ResponseInfo<String> deleteApi(@PathVariable(value = "id") int id) {
        apiService.deleteApi(id);
        return ResponseInfo.successInfo("");
    }

    /**
     * @param apiSuiteId api类别的id
     * @return 获取返回结果
     */
    @GetMapping("/list")
    @ApiOperation(value = "api列表接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Api.class)
    })
    @ApiImplicitParam(name = "apiSuiteId", value = "api类别的id", required = true, dataType = "int")
    public ResponseInfo<List<Api>> findBySuiteId(@RequestParam Integer apiSuiteId) {
        return ResponseInfo.successInfo(apiService.findBySuiteId(apiSuiteId));
    }


    /**
     * debug -api接口
     *
     * @param id 传入apiId
     * @return 获取返回结果
     */
    @PostMapping("/run")
    @ApiOperation(value = "api调试接口")
    @ApiImplicitParam(name = "id", value = "apiId", required = true, dataType = "int")
    public ResponseInfo<ApiRequestResult> runApi(@RequestParam int id) {
        return ResponseInfo.successInfo(apiRequestResultService.insertApiRequestResult(id));
    }

    /**
     * 获取api详情接口
     *
     * @param id apiId
     * @return 获取返回结果
     */
    @GetMapping("/details")
    @ApiOperation(value = "获取api详情接口", notes = "通过id获取api详情")
    @ApiImplicitParam(name = "id", value = "apiId", required = true, dataType = "int")
    public ResponseInfo<Api> getApiDetails(@RequestParam int id) {
        return ResponseInfo.successInfo(apiService.findById(id));
    }


    @PostMapping("/transfer/device/data")
    public ResponseInfo<DataTest> getData(@RequestBody DataTest dataTest) {
        log.info("实时数据:" + JSONObject.toJSONString(dataTest));
        return ResponseInfo.successInfo(dataTest);
    }

    @PostMapping("/transfer/device/alarm")
    public ResponseInfo<AlarmData> getAlarm(@RequestBody AlarmData alarmData) {
        log.info("报警数据:" + JSONObject.toJSONString(alarmData));
        return ResponseInfo.successInfo(alarmData);
    }

    @PostMapping("/transfer/device/fault")
    public ResponseInfo<FaultData> getFault(@RequestBody FaultData faultData) {
        log.info("故障数据:" + JSONObject.toJSONString(faultData));
        return ResponseInfo.successInfo(faultData);
    }

    @PostMapping("/transfer/device/config")
    public ResponseInfo<ConfigData> getConfig(@RequestBody ConfigData configData) {
        log.info("配置数据：" + JSONObject.toJSONString(configData));
        return ResponseInfo.successInfo(configData);
    }

    /**
     * 新租户新增物流2.0数据一条龙服务
     *
     * @param tenantId
     * @return
     */
    @PostMapping("/createData")
    public ResponseInfo<String> createData(@RequestParam(value = "tenantId") int tenantId, @RequestParam(value = "accessToken") String accessToken) {
        apiService.createData(tenantId, accessToken);
        return ResponseInfo.successInfo("");
    }


}
